package com.example.helloworld;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.example.helloworld.view.OriginalPictureView;

public class Gallery extends Activity {

	private Cursor imageCursor;
	private Cursor actualImageCursor;
	private int imageColumnIndex;
	private int actualImageColumnIndex;
	
	private android.widget.Gallery gallery;
	
	private int count;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gallery);
		
		init();
	}
	
	@SuppressWarnings("deprecation")
	private void init() {
		String[] image =	{ MediaStore.Images.Thumbnails._ID };
		imageCursor =		managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, image, null,
								null, MediaStore.Images.Thumbnails.IMAGE_ID);
		
		imageColumnIndex =	imageCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
		count =				imageCursor.getCount();
		
		gallery =			(android.widget.Gallery)findViewById(R.id.gallery);
		gallery.setAdapter(new ImageAdapter(getApplicationContext()));
		gallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				String[] projection =	{MediaStore.Images.Media.DATA};
				actualImageCursor =		managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
				actualImageColumnIndex =
										actualImageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
				actualImageCursor.moveToPosition(arg2);
				String filename =		actualImageCursor.getString(actualImageColumnIndex);
				
				Intent intent =			new Intent(getApplicationContext(), OriginalPictureView.class);
				intent.putExtra("filename", filename);
				
				startActivity(intent);
			}
			
		});
		
	}

	public class ImageAdapter extends BaseAdapter {
		private Context context;
		
		@SuppressWarnings("deprecation")
		private android.widget.Gallery.LayoutParams params;
		
		@SuppressWarnings("deprecation")
		public ImageAdapter(Context c) {
			context =	c;
			
			params =	new android.widget.Gallery.LayoutParams(
				android.widget.Gallery.LayoutParams.WRAP_CONTENT,
				android.widget.Gallery.LayoutParams.WRAP_CONTENT);
		}
		
		@Override
		public int getCount() {
			return count;
		}

		@Override
		public Object getItem(int arg0) {
			return arg0;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView image =	null;
			
			if (convertView == null) {
				image =	new ImageView(context.getApplicationContext());
			} else {
				image =	(ImageView)convertView;
			}
			
			imageCursor.moveToPosition(position);
			int id =	imageCursor.getInt(imageColumnIndex);
			
			image.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, String.valueOf(id)));
			image.setScaleType(ImageView.ScaleType.FIT_XY);
			image.setLayoutParams(params);
			
			return image;
		}
		
	}
}
